home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / MUser17src.lha / MultiUser / src / Support / UserInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  9.0 KB  |  321 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Get Information about one or more Users                            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include    <exec/memory.h>
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <dos/datetime.h>
  16. #include <string.h>
  17. #include <libraries/multiuser.h>
  18. #include <proto/multiuser.h>
  19.  
  20. #include "UserInfo_rev.h"
  21.  
  22.  
  23. char __VersTag__[] = VERSTAG;
  24.  
  25.  
  26. static void __regargs DumpUserInfo(struct muUserInfo *uinfo,
  27.                                               struct muGroupInfo *ginfo, BOOL quick,
  28.                                               BOOL groups, struct muBase *muBase,
  29.                                               struct DosLibrary *DOSBase,
  30.                                               struct ExecBase *SysBase);
  31.  
  32. int __saveds Start(char *arg)
  33. {
  34.     struct ExecBase *SysBase;
  35.     struct DosLibrary *DOSBase;
  36.     struct muBase *muBase = NULL;
  37.     struct RDArgs *args;
  38.     LONG argarray[] = {
  39. #define argUSERID        0
  40. #define argUID            1
  41. #define argGID            2
  42. #define argNAME        3
  43. #define argGROUPID    4
  44. #define argGROUPNAME    5
  45. #define argALL            6
  46. #define argQUICK        7
  47. #define argGROUPS        8
  48.         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  49.     };
  50.     struct muUserInfo *uinfo;
  51.     struct muGroupInfo *ginfo;
  52.     UWORD gid;
  53.     LONG error = NULL;
  54.     int rc = RETURN_OK;
  55.  
  56.     SysBase = *(struct ExecBase **)4;
  57.  
  58.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  59.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  60.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  61.         goto Exit;
  62.     }
  63.  
  64.     args = ReadArgs("USERID,UID/K/N,GID/K/N,NAME/K,GROUPID/K,GROUPNAME/K,"
  65.                          "ALL/S,Q=QUICK/S,GROUPS/S", argarray, NULL);
  66.     if (!args)
  67.         error = IoErr();
  68.     else
  69.         if (uinfo = muAllocUserInfo()) {
  70.             if (ginfo = muAllocGroupInfo()) {
  71.                 if (!argarray[argALL] && !argarray[argUSERID] && 
  72.                      !argarray[argUID] && !argarray[argGID] && !argarray[argNAME] &&
  73.                      !argarray[argGROUPID] && !argarray[argGROUPNAME]) {
  74.                     if (!argarray[argQUICK])
  75.                         PutStr("Dumping information for this user...\n");
  76.                     uinfo->uid = muGetTaskOwner(NULL)>>16;
  77.                     if (muGetUserInfo(uinfo, muKeyType_uid))
  78.                         DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  79.                                          argarray[argGROUPS], muBase, DOSBase, SysBase);
  80.                     else
  81.                         PutStr("No information\n");
  82.                     goto Done;
  83.                 }
  84.  
  85.                 if (argarray[argALL]) {
  86.                     if (!argarray[argQUICK])
  87.                         PutStr("Dumping information for all users...\n");
  88.                     if (muGetUserInfo(uinfo, muKeyType_First))
  89.                         do
  90.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  91.                                              argarray[argGROUPS], muBase, DOSBase,
  92.                                              SysBase);
  93.                         while (muGetUserInfo(uinfo, muKeyType_Next) &&
  94.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  95.                     else
  96.                         PutStr("No information\n");
  97.                     goto Done;
  98.                 }
  99.  
  100.                 if (argarray[argUSERID]) {
  101.                     if (!argarray[argQUICK])
  102.                         VPrintf("Dumping information for user '%s'...\n",
  103.                                   &argarray[argUSERID]);
  104.                     strncpy(uinfo->UserID, (char *)argarray[argUSERID],
  105.                               muUSERIDSIZE-1);
  106.                     uinfo->UserID[muUSERIDSIZE-1] = '\0';
  107.                     if (muGetUserInfo(uinfo, muKeyType_WUserID))
  108.                         do
  109.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  110.                                              argarray[argGROUPS], muBase, DOSBase,
  111.                                              SysBase);
  112.                         while (muGetUserInfo(uinfo, muKeyType_WUserIDNext) &&
  113.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  114.                     else
  115.                         PutStr("No information\n");
  116.                     goto Done;
  117.                 }
  118.  
  119.                 if (argarray[argUID]) {
  120.                     if (!argarray[argQUICK])
  121.                         VPrintf("Dumping information for user %ld...\n",
  122.                                   (LONG *)argarray[argUID]);
  123.                     uinfo->uid = (UWORD)*(LONG *)argarray[argUID];
  124.                     if (muGetUserInfo(uinfo, muKeyType_uid))
  125.                         DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  126.                                          argarray[argGROUPS], muBase, DOSBase, SysBase);
  127.                     else
  128.                         PutStr("No information\n");
  129.                     goto Done;
  130.                 }
  131.  
  132.                 if (argarray[argGID] || argarray[argGROUPID] ||
  133.                      argarray[argGROUPNAME]) {
  134.                     if (argarray[argGID]) {
  135.                         if (!argarray[argQUICK])
  136.                             VPrintf("Dumping information for group %ld...\n",
  137.                                       (LONG *)argarray[argGID]);
  138.                         gid = *(LONG *)argarray[argGID];
  139.                     } else if (argarray[argGROUPID]) {
  140.                         if (!argarray[argQUICK])
  141.                             VPrintf("Dumping information for group '%s'...\n",
  142.                                       &argarray[argGROUPID]);
  143.                         strncpy(ginfo->GroupID, (char *)argarray[argGROUPID],
  144.                                   muGROUPIDSIZE-1);
  145.                         ginfo->GroupID[muGROUPIDSIZE-1] = '\0';
  146.                         if (muGetGroupInfo(ginfo, muKeyType_GroupID))
  147.                             gid = ginfo->gid;
  148.                         else {
  149.                             PutStr("No Information\n");
  150.                             goto Done;
  151.                         }
  152.                     } else {
  153.                         if (!argarray[argQUICK])
  154.                             VPrintf("Dumping information for group '%s'...\n",
  155.                                       &argarray[argGROUPNAME]);
  156.                         strncpy(ginfo->GroupName, (char *)argarray[argGROUPNAME],
  157.                                   muGROUPNAMESIZE-1);
  158.                         ginfo->GroupID[muGROUPNAMESIZE-1] = '\0';
  159.                         if (muGetGroupInfo(ginfo, muKeyType_GroupName))
  160.                             gid = ginfo->gid;
  161.                         else {
  162.                             PutStr("No Information\n");
  163.                             goto Done;
  164.                         }
  165.                     }
  166.                     uinfo->gid = gid;
  167.                     if (muGetUserInfo(uinfo, muKeyType_gid))
  168.                         do
  169.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  170.                                              argarray[argGROUPS], muBase, DOSBase,
  171.                                              SysBase);
  172.                         while (muGetUserInfo(uinfo, muKeyType_gidNext) &&
  173.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  174.                     else
  175.                         PutStr("No information\n");
  176.                     goto Done;
  177.                 }
  178.  
  179.                 if (argarray[argNAME]) {
  180.                     if (!argarray[argQUICK])
  181.                         VPrintf("Dumping information for user '%s'...\n",
  182.                                   &argarray[argNAME]);
  183.                     strncpy(uinfo->UserName, (char *)argarray[argNAME],
  184.                               muUSERNAMESIZE-1);
  185.                     uinfo->UserName[muUSERNAMESIZE-1] = '\0';
  186.                     if (muGetUserInfo(uinfo, muKeyType_WUserName))
  187.                         do
  188.                             DumpUserInfo(uinfo, ginfo, argarray[argQUICK],
  189.                                              argarray[argGROUPS], muBase, DOSBase,
  190.                                              SysBase);
  191.                         while (muGetUserInfo(uinfo, muKeyType_WUserNameNext) &&
  192.                                  !CheckSignal(SIGBREAKF_CTRL_C));
  193.                     else
  194.                         PutStr("No information\n");
  195.                     goto Done;
  196.                 }
  197.  
  198. Done:            muFreeGroupInfo(ginfo);
  199.             } else
  200.                 error = IoErr();
  201.             muFreeUserInfo(uinfo);
  202.         } else
  203.             error = IoErr();
  204.     FreeArgs(args);
  205.     if (error) {
  206.         PrintFault(error, NULL);
  207.         rc = RETURN_ERROR;
  208.     }
  209.  
  210. Exit:
  211.     CloseLibrary((struct Library *)muBase);
  212.     CloseLibrary((struct Library *)DOSBase);
  213.  
  214.     return(rc);
  215. }
  216.  
  217.  
  218.     /*
  219.      *        Dump User Information
  220.      */
  221.  
  222. static void __regargs DumpUserInfo(struct muUserInfo *uinfo,
  223.                                               struct muGroupInfo *ginfo, BOOL quick,
  224.                                               BOOL groups, struct muBase *muBase,
  225.                                               struct DosLibrary *DOSBase,
  226.                                               struct ExecBase *SysBase)
  227. {
  228.     LONG stream[6];
  229.     BPTR dir, file;
  230.     char buffer[256];
  231.     BOOL neverloggedin = TRUE;
  232.     int i;
  233.  
  234.     stream[0] = (LONG)uinfo->UserID;
  235.     stream[1] = uinfo->uid;
  236.     ginfo->gid = uinfo->gid;
  237.     if (muGetGroupInfo(ginfo, muKeyType_gid))
  238.         stream[2] = (LONG)ginfo->GroupID;
  239.     else
  240.         stream[2] = (LONG)"???";
  241.     stream[3] = uinfo->gid;
  242.     stream[4] = (LONG)uinfo->UserName;
  243.     stream[5] = (LONG)uinfo->HomeDir;
  244.  
  245.     if (quick)
  246.         VPrintf("%s\n", stream);
  247.     else {
  248.         VPrintf("\nUser '%s' (uid %ld), group '%s' (gid %ld)\n"
  249.                   "In real life '%s'\n"
  250.                   "Home Directory '%s'\n", stream);
  251.  
  252.         if (groups)
  253.             if (uinfo->NumSecGroups) {
  254.                 ginfo->gid = uinfo->SecGroups[0];
  255.                 if (muGetGroupInfo(ginfo, muKeyType_gid))
  256.                     stream[0] = (LONG)ginfo->GroupID;
  257.                 else
  258.                     stream[0] = (LONG)"???";
  259.                 stream[1] = uinfo->SecGroups[0];
  260.                 VPrintf("Secondary groups: '%s' (gid %ld)", stream);
  261.                 for (i = 1; i < uinfo->NumSecGroups; i++) {
  262.                     ginfo->gid = uinfo->SecGroups[i];
  263.                     if (muGetGroupInfo(ginfo, muKeyType_gid))
  264.                         stream[0] = (LONG)ginfo->GroupID;
  265.                     else
  266.                         stream[0] = (LONG)"???";
  267.                     stream[1] = uinfo->SecGroups[i];
  268.                     VPrintf(", '%s' (gid %ld)", stream);
  269.                 }
  270.                 PutStr("\n");
  271.             } else
  272.                 PutStr("No secondary groups.\n");
  273.  
  274.         if (dir = Lock(uinfo->HomeDir, SHARED_LOCK)) {
  275.             dir = CurrentDir(dir);
  276.             if (file = Open(muLastLogin_FileName, MODE_OLDFILE)) {
  277.                 if (FGets(file, buffer, 3*LEN_DATSTRING+2))
  278.                     neverloggedin = FALSE;
  279.                 Close(file);
  280.             }
  281.             if (neverloggedin)
  282.                 PutStr("Never logged in.\n");
  283.             else {
  284.                 stream[0] = (LONG)buffer;
  285.                 stream[1] = (LONG)"";
  286.                 stream[2] = (LONG)"";
  287.                 for (i = 0; buffer[i] && (buffer[i] != ' '); i++);
  288.                 if (buffer[i]) {
  289.                     buffer[i++] = '\0';
  290.                     while (buffer[i] == ' ')
  291.                         i++;
  292.                     stream[1] = (LONG)&buffer[i];
  293.                     while (buffer[i] && (buffer[i] != ' '))
  294.                         i++;
  295.                     if (buffer[i]) {
  296.                         buffer[i++] = '\0';
  297.                         while (buffer[i] == ' ')
  298.                             i++;
  299.                         stream[2] = (LONG)&buffer[i];
  300.                         while (buffer[i] && (buffer[i] != ' ') &&
  301.                                  (buffer[i] != '\n'))
  302.                             i++;
  303.                         buffer[i] = '\0';
  304.                     }
  305.                 }
  306.                 VPrintf("Last login on %s, %s at %s.\n", stream);
  307.             }
  308.             if (file = Open(".plan", MODE_OLDFILE)) {
  309.                 PutStr("Plan:\n");
  310.                 while (!CheckSignal(SIGBREAKF_CTRL_C) &&
  311.                          FGets(file, buffer, 255))            /* V39: use 256 */
  312.                     PutStr(buffer);
  313.                 Close(file);
  314.             } else
  315.                 PutStr("No plan.\n");
  316.             UnLock(CurrentDir(dir));
  317.         } else
  318.             PutStr("No access to last login or plan.\n");
  319.     }
  320. }
  321.